﻿<?php
require_once ($_SERVER["DOCUMENT_ROOT"] ."/include/config.php");
require_once ("sql.php");

class CableController extends BaseController {

	static public function GetCablesListArray() {
		self::SQL_connect();
		$cables = self::GetCablesList(false);
		self::SQL_close();
		return $cables;
	}

	static public function ProcessRequest($request) {
		self::SQL_connect();

		if ($request == "params") {
			self::GetParamsTable();
		} else if ($request == "list")
			self::GetCablesList();
		else if (isset($_POST["id"])) {
			$id = $_POST["id"];
			if ($request == "get")
				self::GetCable($id);
			else if ($request == "del") {
				if (isset($_POST["id"]))
					self::DeleteCable($id);
			} else if ($request == "set") {
				if (isset($_POST["id"]) && isset($_POST["data"]))
					self::UpdateCable($id, $_POST["data"]);
			}
		}
		self::SQL_close();
	}

	static private function GetParamsTable() {

		$query = "SELECT * FROM cables_param_type;";
		$result = self::$db_link -> query($query);
		$params = Array();
		while ($row = $result -> fetch_object()) {
			$params[$row -> id] = $row -> title;
		}
		$result -> close();

		echo json_encode($params);
	}

	static private function GetCable($id) {

		$query = "SELECT * FROM cables WHERE id = '$id';";
		$query .= "SELECT modules.id as mid, modules.number as mnum, modules.color as mcol, fiber.id as fid, fiber.number as fnum, fiber.color as fcol FROM itex_network.modules LEFT JOIN fiber on fiber.id_module = modules.id WHERE modules.id_cable='$id' ORDER BY modules.number;";
		$query .= "Call GetCableParams('$id');";
		$cable = Array();
		$cable["params"] = Array();
		$cable["modules"] = (object) Array();
		if (self::$db_link -> multi_query($query)) {

			if ($result = self::$db_link -> store_result()) {
				$row = $result -> fetch_object();
				$cable["id"] = $row -> id;
				$cable["title"] = $row -> title;
				$cable["description"] = $row -> description;
				$cable["attenuation"] = $row->attenuation;
				$result -> free();
				self::$db_link -> next_result();
			}

			if ($result = self::$db_link -> store_result()) {
				$modules = Array();
				while ($row = $result -> fetch_object()) {
					if (!isset($modules[$row -> mid]))
						$modules[$row -> mid] = (object) array('number' => $row -> mnum, 'color' => $row -> mcol, 'fiber' => array());
					if ($row -> fid != null)
						$modules[$row -> mid] -> fiber[] = array($row -> fnum, $row -> fcol, $row -> fid);
				}
				$cable["modules"] = (object)$modules;
				$result -> free();
				self::$db_link -> next_result();
			}
			if ($result = self::$db_link -> store_result()) {
				while ($row = $result -> fetch_object())
					$cable["params"][$row -> id] = $row -> value;
				$result -> free();
			}
		}

		echo json_encode($cable);
	}

	static private function GetCablesList($echo = true) {

		$query = "SELECT id, title FROM cables ORDER BY title; ";
		$cables = Array();
		$result = self::$db_link -> query($query);
		while ($row = $result -> fetch_object())
			$cables[$row -> id] = $row -> title;
		$result -> close();

		if ($echo)
			echo json_encode($cables);
		return $cables;
	}

	static private function DeleteCable($id) {
		$query = "DELETE FROM cables WHERE id = '$id';";
		self::ProcessQuery($query);
	}

	static private function UpdateCable($id, $_data) {
		$data = json_decode($_data);
		$title = $data -> title;
		$desc = $data -> description;
		$attenuation = $data->attenuation;

		$query = "INSERT INTO cables (id, title, description, attenuation) VALUES ('$id', '$title', '$desc', $attenuation)
	  	ON DUPLICATE KEY UPDATE title = '$title', description = '$desc', attenuation = $attenuation;";

		foreach ($data->params as $key => $param) {
			$query .= "INSERT INTO cables_param (id_cable, id_param, param_value) VALUES ('$id', '" . $param -> id . "', '" . $param -> value . "')
			ON DUPLICATE KEY UPDATE param_value = '" . $param -> value . "';";
		}

		foreach ($data->modules as $key => $value) {
			if (isset($value->delete)) $query .= "DELETE FROM modules WHERE id='$key';";
			else {
				$query .= "INSERT INTO modules (id, number, color, id_cable) VALUES ('$key', '$value->number', '$value->color', '$id')
				ON DUPLICATE KEY UPDATE number = '$value->number', color = '$value->color', id_cable = '$id';";
				$fibers = array();
				foreach ($value->fiber as $idx => $fiber) {
					$query .= "INSERT INTO fiber (id, number, color,id_module) 
					VALUES ('$fiber[2]', '$fiber[0]', '$fiber[1]', '$key')
					ON DUPLICATE KEY UPDATE number = '$fiber[0]', color = '$fiber[1]', id_module = '$key';";
					array_push($fibers, $fiber[2]);
				}
				$query .= "DELETE from fiber WHERE id not IN ('".implode("', '", $fibers)."') and id_module = '$key';";
			}
		}
		self::ProcessQuery($query);
	}
}

CableController::$config = $config;
if (isset($_POST["op"])) 
	CableController::ProcessRequest($_POST["op"]);

?>